﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Service.Impl
{
    public class MailService : IMailService
    {
        private string _host, _user, _pass;
        private int _port;
        private bool _ssl;

        public void InitParameters(string host, int port, bool ssl, string user, string pass)
        {
            _host = host; _port = port; _ssl = ssl; _user = user; _pass = pass;
        }

        public void InitParameters()
        {
            _host = "smtp.live.com";
            _port = 587;
            _ssl = true;
            _user = "ecofast@outlook.com";
            _pass = "Chevre123456";
        }

        public void SendMail(string from, string subject, string body, params string[] to)
        {
            if (_host == null)
            {
                InitParameters();
            }
            // Initialisation d'un serveur SMTP
            SmtpClient smtp = new SmtpClient();
            smtp.Host = _host;
            smtp.Port = _port;
            smtp.EnableSsl = _ssl;
            smtp.Credentials = new System.Net.NetworkCredential(_user, _pass);

            // Constructeur du message
            var mess = new MailMessage()
            {
                From = new MailAddress(from ?? _user),
                Subject = subject,
                Body = body,
            };
            foreach (var adress in to)
            {
                mess.To.Add(new MailAddress(adress));
            }
            // Envoi du message
            smtp.Send(mess);
        }

    }
}
